package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.system.domain.JingfeiPiciEx;
import com.ruoyi.system.service.IJisuanService;
import com.ruoyi.system.tools.BooleanMessage;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.JingfeiJingfeipici;
import com.ruoyi.system.service.IJingfeiJingfeipiciService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * jingfeipiciController
 * 
 * @author ruoyi
 * @date 2024-07-11
 */
@Controller
@RequestMapping("/system/jingfeipici")
public class JingfeiJingfeipiciController extends BaseController
{
    private String prefix = "system/jingfeipici";

    @Autowired
    private IJingfeiJingfeipiciService jingfeiJingfeipiciService;

    @Autowired
    private IJisuanService jisuanService;

    @RequiresPermissions("system:jingfeipici:view")
    @GetMapping()
    public String jingfeipici()
    {
        return prefix + "/jingfeipici";
    }

    /**
     * 查询jingfeipici列表
     */
    @RequiresPermissions("system:jingfeipici:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JingfeiJingfeipici jingfeiJingfeipici)
    {
        startPage();
        List<JingfeiJingfeipici> list = jingfeiJingfeipiciService.selectJingfeiJingfeipiciList(jingfeiJingfeipici);
        return getDataTable(list);
    }

    /**
     * 导出jingfeipici列表
     */
    @RequiresPermissions("system:jingfeipici:export")
    @Log(title = "jingfeipici", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JingfeiJingfeipici jingfeiJingfeipici)
    {
        List<JingfeiJingfeipici> list = jingfeiJingfeipiciService.selectJingfeiJingfeipiciList(jingfeiJingfeipici);
        ExcelUtil<JingfeiJingfeipici> util = new ExcelUtil<JingfeiJingfeipici>(JingfeiJingfeipici.class);
        return util.exportExcel(list, "jingfeipici数据");
    }

    /**
     * 新增jingfeipici
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存jingfeipici
     */
    @RequiresPermissions("system:jingfeipici:add")
    @Log(title = "jingfeipici", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JingfeiJingfeipici jingfeiJingfeipici)
    {
        return toAjax(jingfeiJingfeipiciService.insertJingfeiJingfeipici(jingfeiJingfeipici));
    }

    /**
     * 修改jingfeipici
     */
    @RequiresPermissions("system:jingfeipici:edit")
    @GetMapping("/edit/{jingfeipiciid}")
    public String edit(@PathVariable("jingfeipiciid") String jingfeipiciid, ModelMap mmap)
    {
        JingfeiJingfeipici jingfeiJingfeipici = jingfeiJingfeipiciService.selectJingfeiJingfeipiciByJingfeipiciid(jingfeipiciid);
        mmap.put("jingfeiJingfeipici", jingfeiJingfeipici);
        return prefix + "/edit";
    }

    /**
     * 修改保存jingfeipici
     */
    @RequiresPermissions("system:jingfeipici:edit")
    @Log(title = "jingfeipici", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JingfeiJingfeipici jingfeiJingfeipici)
    {
        return toAjax(jingfeiJingfeipiciService.updateJingfeiJingfeipici(jingfeiJingfeipici));
    }

    /**
     * 删除jingfeipici
     */
    @RequiresPermissions("system:jingfeipici:remove")
    @Log(title = "jingfeipici", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jingfeiJingfeipiciService.deleteJingfeiJingfeipiciByJingfeipiciids(ids));
    }

    @PostMapping("/customlist")
    @ResponseBody
    public JsonResult<?> customlist(@RequestParam("deptid") String deptid,@RequestParam("piciname") String piciname,@RequestParam("zhuangtai") String zhuangtai)
    {
        try {
            return JsonResult.getSuccessResult(jingfeiJingfeipiciService.customlist(deptid,piciname,zhuangtai));
        } catch (RuntimeException e) {
            return JsonResult.getErrorResult(e.getMessage());
        }
    }


    @PostMapping("/findwffbydeptid")
    @ResponseBody
    public JsonResult<?> findWFFByDeptId(@RequestParam("deptid") String deptid)
    {
        try {
            return JsonResult.getSuccessResult(jingfeiJingfeipiciService.findWFFByDeptId(deptid));
        } catch (RuntimeException e) {
            return JsonResult.getErrorResult(e.getMessage());
        }
    }


    @Log(title = "jingfeipici", businessType = BusinessType.INSERT)
    @PostMapping("/customadd")
    @ResponseBody
    public JsonResult<?> customadd(@RequestBody JingfeiPiciEx JingfeipiciEx)
    {
        try {
            boolean ok = jingfeiJingfeipiciService.customadd(JingfeipiciEx);
            return ok?JsonResult.getSuccessResult("新增经费批次成功"):JsonResult.getErrorResult("新增经费批次失败");
        } catch (RuntimeException e) {
            return JsonResult.getErrorResult(e.getMessage());
        }
    }

    @Log(title = "zjzxjingfeijisuan", businessType = BusinessType.INSERT)
    @PostMapping("/zjzxjingfeijisuan")
    @ResponseBody
    public JsonResult<?> zjzxjingfeijisuan(@RequestParam("piciid") String piciid, HttpServletRequest request)
    {
        try {
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString(); // 单位id
            if(!RuoYiConfig.getZjzxdeptid().equals(deptid)){
                return JsonResult.getErrorResult("当前单位无此权限");
            }
            BooleanMessage ok = jisuanService.ZJZXJingfeijisuan(piciid,deptid);
            return ok.isOk()?JsonResult.getSuccessResult("计算成功"):JsonResult.getErrorResult(ok.getMessage().toString());
        } catch (RuntimeException e) {
            return JsonResult.getErrorResult(e.getMessage());
        }
    }


    // 专家中心经费明细
    @PostMapping("/zjzxjfmx")
    @ResponseBody
    public JsonResult<?> zjzxjfmx(@RequestParam("piciid") String piciid,@RequestParam("isshangjifafang") String isshangjifafang,
                                  @RequestParam("isshoukuan") String isshoukuan,@RequestParam("deptname") String deptname, HttpServletRequest request)
    {
        try {
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString(); // 单位id

            return JsonResult.getSuccessResult(jisuanService.zjzxjfmx(piciid,deptid,isshangjifafang,isshoukuan,deptname)); // 只计算单位发的钱
        } catch (RuntimeException e) {
            return JsonResult.getErrorResult(e.getMessage());
        }
    }

    @Log(title = "单位经费发放列表", businessType = BusinessType.UPDATE)
    @PostMapping("/pici_list")
    @ResponseBody
    public JsonResult pici_list(@RequestParam("piciname")String ppPiciName,HttpServletRequest request) {

        try {
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString();
            return JsonResult.getSuccessResult(jingfeiJingfeipiciService.findPCByDeptIdAndPiciName(deptid,ppPiciName));
        } catch (Exception e) {
            logger.error("jingfeipici -> pici_list: "+e.getMessage());
            return JsonResult.getErrorResult("经费批次列表查询失败");
        }
    }

    @Log(title = "经费批次", businessType = BusinessType.DELETE)
    @PostMapping("/customremove")
    @ResponseBody
    public JsonResult<?> customremove(@RequestParam("piciid")String ppPiciId)
    {
        try {
            return jingfeiJingfeipiciService.deleteJingfeipici(ppPiciId)==true?JsonResult.getSuccessResult("删除津贴批次成功！"):JsonResult.getErrorResult("删除津贴批次失败！");
        } catch (RuntimeException e) {
            return JsonResult.getErrorResult(e.getMessage());
        }
    }
}
